<?php

class Administrator_Newsv2Controller extends Zend_Controller_Action {
	public $deniedIds = '';
    public function init() {
        
    }

    public function indexAction() {
        $objNewsv2 = new HT_Model_administrator_models_newsv2();
        $do = @$this->_request->getParam('do');
        $id = (int) $this->_request->getParam('id');
        if ($do == 'delete' && $id > 0) {
            $this->deleteNewsv2($id);
        } elseif ($do == 'list') {
            $this->getListNewsv2();
        } else {
            $keyword = $this->_request->getParam('keyword');
            $this->view->keyword = $keyword;
        }
        $this->view->inlineScript()->appendFile(WEB_PATH . '/application/modules/administrator/views/scripts/newsv2/index.js');
    }

    public function updateAction() {
    	$objUtil   = new HT_Model_administrator_models_utility();
        $objNewsv2 = new HT_Model_administrator_models_newsv2();
        $do = @$this->_request->getParam('do');
        $id = (int) $this->_request->getParam('id');
        $ustatus = (int) $this->_request->getParam('ustatus');
        $category_ids = $this->_request->getParam('category_ids');
        $delete_image = @$this->_request->getParam('delete_image');
        
        if ($do == 'submit') {
        	$image = $objUtil->uploadFile('image',NEWS_IMAGE_PATH,MAX_IMAGE_FILE_SIZE,IMAGE_TYPE_ALLOW);
        	
            $data = array();
            if(!in_array($image,array(1,2,3,4))){
            	$data['image'] = $image;
            }
            $data['image_description'] = $this->_request->getParam('image_description');
            $data['title_vn'] 		= $this->_request->getParam('title_vn');
            $data['desc_vn'] 		= $this->_request->getParam('desc_vn');
            $data['status'] 		= $this->_request->getParam('status');
            $data['content_vn'] 	= $this->_request->getParam('content_vn');
            $data['created'] 		= date('Y-m-d H:i:s');
            if($delete_image) $data['image'] = null;          
            
            if ($id > 0) {
                $return = $objNewsv2->updateData($data,$id);
            } else {
                $return = $objNewsv2->addData($data);
                if($return >0){
                	$id = $return;
                	$objNewsv2->updateCategories($category_ids,$id);
                }
            }
            
            if($return >0){
            	$this->_redirect(WEB_PATH.'/administrator/newsv2/update?ustatus=1&id='.$id);
            }else{
            	$this->_redirect(WEB_PATH.'/administrator/newsv2/update?ustatus='.$return.'&id='.$id);
            }
        } elseif ($id > 0) {
            $this->view->newsv2 = $objNewsv2->getNewsv2($id);
        }
        
        $groupList		   = $objNewsv2->getGroupList();
        
        $this->view->id 		= $id;
        $this->view->ustatus 	= $ustatus;
        $this->view->groupList 	= $groupList;
        $this->view->inlineScript()->appendFile(WEB_PATH . '/application/modules/administrator/views/scripts/newsv2/update.js');
    }

    function deleteNewsv2($id) {
        $objNewsv2 = new HT_Model_administrator_models_newsv2();
        echo $objNewsv2->delete("id_news=".(int) $id);
        die();
    }

	function getListNewsv2(){
		$objUtil 		= new HT_Model_administrator_models_utility();
		$objConvert		= new HT_Model_administrator_models_convert();
		$objNewsv2 	= new HT_Model_administrator_models_newsv2();
		$keyword 		= trim($this->_request->getParam('keyword'));
		$page 			= (int)$this->_request->getParam('page');
		$searchFor 		= $this->_request->getParam('search_for');
		
		$size 			= PAGING_SIZE;
		if (!is_numeric($page) || $page <= 0) {
			$page = 1;
		}
		$start = $page * $size - $size;
		
		$filter = array('denied_ids'=>$this->deniedIds);
		if($keyword) $filter['keyword'] = $keyword;
		if($searchFor == 'not_connected' || $searchFor == 'connected') $filter['search_for'] = $searchFor;
		
		
		$totalRecord = $objNewsv2->getListNewsv2_nb($filter);
		$listNews = $objNewsv2->getListNewsv2($start,$size,$filter);
		$paging = trim($objUtil->paging($page, $size, $totalRecord));
	
		$ajaxData = null;
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
		$ajaxData .= '<table cellspacing="0" class="tablesorter tablesorterBorder">';
		$ajaxData .= '<thead>';
		$ajaxData .= '<tr>';
		$ajaxData .= '<th width="15">STT</th>';
		$ajaxData .= '<th width="400">Tiêu đề tiếng việt</th>';
		$ajaxData .= '<th width="400">Mô tả tiếng việt</th>';
		$ajaxData .= '<th width="100">Ngày tạo</th>';
		$ajaxData .= '<th style="white-space: nowrap;padding-right: 5px;" align="center">Điều khiển</th>';
		$ajaxData .= '</tr>';
		$ajaxData .= '</thead>';
	
		$i=0;
		foreach($listNews as $news){
			$url = WEB_PATH."/chi-tiet/".$objConvert->utf8_to_url($news['newsTitle']).'-'.$news['newsId'].".html";
			$i++;
			$trClass = null;
			if($i%2 == 1) $trClass = ' class="altrow"';
			$ajaxData .= '<tr id="'.$news['newsId'].'" '.$trClass.'>';
			$ajaxData .= '<td align="center">'.$i.'</td>';
			$ajaxData .= '<td><a href="'.$url.'" target="_blank">'.$news['newsTitle'].'</a></td>';
			$ajaxData .= '<td>'.$news['description'].'</td>';
			$ajaxData .= '<td>'.$objUtil->normalDate($news['created']).'</td>';
			$ajaxData .= '<td style="white-space: nowrap" align="center">';
			$ajaxData .= '<a href="#" onclick="deleteNews('.$news['newsId'].')">Xóa</a> | <a href="'.WEB_PATH.'/administrator/newsv2/update/?id='.$news['newsId'].'">Cập nhật</a>';
			$ajaxData .= '</td>';
			$ajaxData .= '</tr>';
		}
		$ajaxData .= '</tbody>';
		$ajaxData .= '</table>';
		$ajaxData .= '</div>';
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
	
		$searchResult = "Có <b>".number_format($totalRecord, 0, '.', ',')."</b> Kết quả phù hợp với dữ liệu tìm kiếm";
		echo $ajaxData.':::'.$searchResult; die();
	}

}
